package cadastro;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.sql.SQLException;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRootPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;

import principal.JFPrincipal;
import dao.UsuarioDAO;
import entidade.Usuario;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;

import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.Bindings;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.swingbinding.JTableBinding;
import org.jdesktop.swingbinding.SwingBindings;

import controller.UsuarioList;

import javax.swing.DefaultComboBoxModel;

@SuppressWarnings({"serial","rawtypes","unchecked"})
public class JDUsuario extends JDialog {

	private UsuarioDAO udao;
	private UsuarioList m_list;
	private JPanel contentPane;
	private JTabbedPane tPane;
	private JPanel listPane;
	private JPanel controlPane;
	private JPanel fieldPane;
	private JPanel buttonPane;
	private JPanel dataPanel;
	private JPanel btnPane;
	private JScrollPane scrollPane;
	private JTable table;
	private JTextField tfNome;
	private JTextField tfLogin;
	private JPasswordField pfSenha;
	private JComboBox cbAdmin;

	public JDUsuario(JFPrincipal fPrincipal) throws ClassNotFoundException,
			SQLException {
		super(fPrincipal);

		udao = new UsuarioDAO();
		m_list = new UsuarioList(udao.list());

		setBounds(100, 100, 450, 300);
		setLocationRelativeTo(this);
		setTitle("Ger\u00EAncia de Usu\u00E1rios");
		setModal(true);

		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout());
		setContentPane(contentPane);
		
		tPane = new JTabbedPane();
		contentPane.add(tPane, BorderLayout.CENTER);

		{
			listPane = new JPanel(new BorderLayout());
			tPane.addTab("Lista", listPane);
			
			controlPane = new JPanel(new BorderLayout());
			listPane.add(controlPane, BorderLayout.CENTER);
			
			scrollPane = new JScrollPane();
			controlPane.add(scrollPane, BorderLayout.CENTER);

			table = new JTable();
			table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			scrollPane.setViewportView(table);
			
			buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT, 2, 5));
			listPane.add(buttonPane, BorderLayout.SOUTH);

			JButton btnNovo = new JButton("Novo");
			btnNovo.setMnemonic('N');
			btnNovo.setContentAreaFilled(false);
			btnNovo.setIcon(new ImageIcon(JDUsuario.class
					.getResource("/img/textfield.png")));
			btnNovo.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					Usuario u = new Usuario();
					udao.salve(u);
					m_list.addUsuario(u);
					table.repaint();
					table.changeSelection(table.getRowCount()-1,table.getRowCount(),false,false);
					tPane.setSelectedIndex(1);
				}
			});
			buttonPane.add(btnNovo);

			JButton btnEditar = new JButton("Editar");
			btnEditar.setIcon(new ImageIcon(JDUsuario.class.getResource("/img/textfield_rename.png")));
			btnEditar.setMnemonic('E');
			btnEditar.setContentAreaFilled(false);
			btnEditar.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					// TODO Auto-generated method stub
					tPane.setSelectedIndex(1);
				}
			});
			buttonPane.add(btnEditar);
			
			JButton btnExcluir = new JButton("Excluir");
			btnExcluir.setMnemonic('X');
			btnExcluir.setContentAreaFilled(false);
			btnExcluir.setIcon(new ImageIcon(JDUsuario.class
					.getResource("/img/delete.png")));
			buttonPane.add(btnExcluir);
		}
		
		{
			dataPanel = new JPanel(new BorderLayout());
			tPane.addTab("Dados", dataPanel);
			
			fieldPane = new JPanel(new GridBagLayout());
			dataPanel.add(fieldPane, BorderLayout.CENTER);

			JLabel lbNome = new JLabel("Nome:");
			fieldPane.add(lbNome, new GridBagConstraints(0, 0, 1, 1, 0, 0,
					GridBagConstraints.EAST, GridBagConstraints.EAST,
					new Insets(2, 2, 2, 2), 0, 0));

			tfNome = new JTextField();
			fieldPane.add(tfNome, new GridBagConstraints(1, 0, 1, 1, 1, 0,
					GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
					new Insets(2, 2, 2, 2), 0, 0));

			JLabel lbLogin = new JLabel("Login:");
			fieldPane.add(lbLogin, new GridBagConstraints(0, 1, 1, 1, 0, 0,
					GridBagConstraints.EAST, GridBagConstraints.EAST,
					new Insets(2, 2, 2, 2), 0, 0));

			tfLogin = new JTextField();
			fieldPane.add(tfLogin, new GridBagConstraints(1, 1, 1, 1, 1, 0,
					GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
					new Insets(2, 2, 2, 2), 0, 0));

			JLabel lbSenha = new JLabel("Senha:");
			fieldPane.add(lbSenha, new GridBagConstraints(0, 2, 1, 1, 0, 0,
					GridBagConstraints.EAST, GridBagConstraints.EAST,
					new Insets(2, 2, 2, 2), 0, 0));

			pfSenha = new JPasswordField();
			fieldPane.add(pfSenha, new GridBagConstraints(1, 2, 1, 1, 1, 0,
					GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
					new Insets(2, 2, 2, 2), 0, 0));
			
			JLabel lbAdmin = new JLabel("Admin:");
			fieldPane.add(lbAdmin, new GridBagConstraints(0, 3, 1, 1, 0, 0,
					GridBagConstraints.EAST, GridBagConstraints.EAST,
					new Insets(2, 2, 2, 2), 0, 0));
			
			cbAdmin = new JComboBox();
			cbAdmin.setModel(new DefaultComboBoxModel(new String[] {"N\u00E3o", "Sim"}));
			fieldPane.add(cbAdmin, new GridBagConstraints(1, 3, 1, 1, 1, 0,
					GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
					new Insets(2, 2, 2, 2), 0, 0));
			
			btnPane = new JPanel(new FlowLayout(FlowLayout.RIGHT, 2, 5));
			dataPanel.add(btnPane, BorderLayout.SOUTH);
			
			JButton btnSalvar = new JButton("Salvar");
			btnSalvar.setMnemonic('S');
			btnSalvar.setContentAreaFilled(false);
			btnSalvar.setIcon(new ImageIcon(JDUsuario.class
					.getResource("/img/textfield_rename.png")));
			btnSalvar.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					m_list.getUsuario(table.getSelectedRow()).setAdministrador(cbAdmin.getSelectedIndex());
					udao.update(m_list.getUsuario(table.getSelectedRow()));
					table.repaint();
					tPane.setSelectedIndex(0);
				}
			});
			btnPane.add(btnSalvar);
			
			JButton btnCancelar = new JButton("Cancelar");
			btnCancelar.setIcon(new ImageIcon(JDUsuario.class.getResource("/img/cross.png")));
			btnCancelar.setMnemonic('C');
			btnCancelar.setContentAreaFilled(false);
			btnCancelar.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					// TODO Auto-generated method stub
					tPane.setSelectedIndex(0);
				}
			});
			btnPane.add(btnCancelar);
			
		}
		
		initDataBindings();
	}

	protected JRootPane createRootPane() {
		ActionListener actionListener = new ActionListener() {
			public void actionPerformed(ActionEvent actionEvent) {
				dispose();
			}
		};
		JRootPane rootPane = new JRootPane();
		KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
		rootPane.registerKeyboardAction(actionListener, stroke,
				JComponent.WHEN_IN_FOCUSED_WINDOW);
		return rootPane;
	}
	protected void initDataBindings() {
		BeanProperty<UsuarioList, List<Usuario>> usuarioListBeanProperty = BeanProperty.create("usuarios");
		JTableBinding<Usuario, UsuarioList, JTable> jTableBinding = SwingBindings.createJTableBinding(UpdateStrategy.READ, m_list, usuarioListBeanProperty, table);
		//
		BeanProperty<Usuario, Integer> usuarioBeanProperty = BeanProperty.create("codigo");
		jTableBinding.addColumnBinding(usuarioBeanProperty).setColumnName("C\u00F3digo").setColumnClass(Integer.class);
		//
		BeanProperty<Usuario, String> usuarioBeanProperty_1 = BeanProperty.create("nome");
		jTableBinding.addColumnBinding(usuarioBeanProperty_1).setColumnName("Nome");
		//
		BeanProperty<Usuario, String> usuarioBeanProperty_2 = BeanProperty.create("login");
		jTableBinding.addColumnBinding(usuarioBeanProperty_2).setColumnName("Login");
		//
		jTableBinding.bind();
		//
		BeanProperty<JTable, String> jTableBeanProperty = BeanProperty.create("selectedElement.nome");
		BeanProperty<JTextField, String> jTextFieldBeanProperty = BeanProperty.create("text");
		AutoBinding<JTable, String, JTextField, String> autoBinding = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, table, jTableBeanProperty, tfNome, jTextFieldBeanProperty);
		autoBinding.bind();
		//
		BeanProperty<JTable, String> jTableBeanProperty_1 = BeanProperty.create("selectedElement.login");
		BeanProperty<JTextField, String> jTextFieldBeanProperty_1 = BeanProperty.create("text");
		AutoBinding<JTable, String, JTextField, String> autoBinding_1 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, table, jTableBeanProperty_1, tfLogin, jTextFieldBeanProperty_1);
		autoBinding_1.bind();
		//
		BeanProperty<JTable, String> jTableBeanProperty_2 = BeanProperty.create("selectedElement.senha");
		BeanProperty<JPasswordField, String> jPasswordFieldBeanProperty = BeanProperty.create("text");
		AutoBinding<JTable, String, JPasswordField, String> autoBinding_2 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, table, jTableBeanProperty_2, pfSenha, jPasswordFieldBeanProperty);
		autoBinding_2.bind();
		//
		BeanProperty<JTable, Integer> jTableBeanProperty_3 = BeanProperty.create("selectedElement.administrador");
		BeanProperty<JComboBox, Integer> jComboBoxBeanProperty = BeanProperty.create("selectedIndex");
		AutoBinding<JTable, Integer, JComboBox, Integer> autoBinding_3 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, table, jTableBeanProperty_3, cbAdmin, jComboBoxBeanProperty);
		autoBinding_3.bind();
	}
}